package com.watch.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.watch.system.common.util.DateUtils;
import com.watch.system.converter.CarUserConverter;
import com.watch.system.mapper.CarAuthMapper;
import com.watch.system.mapper.CarUserMapper;
import com.watch.system.mapper.ResourcesMapper;
import com.watch.system.model.bo.UserFormBO;
import com.watch.system.model.dto.UserAuthInfo;
import com.watch.system.model.entity.CarResources;
import com.watch.system.model.entity.CarUser;
import com.watch.system.model.form.*;
import com.watch.system.model.query.CarAuthPageQuery;
import com.watch.system.model.query.CarResoucesPageQuery;
import com.watch.system.model.query.CarUserPageQuery;
import com.watch.system.model.query.UserPageQuery;
import com.watch.system.model.vo.*;
import com.watch.system.security.util.SecurityUtils;
import com.watch.system.service.CarUserService;
import com.watch.system.service.ResourcesService;
import com.watch.system.service.SysMenuService;
import com.watch.system.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户业务实现类
 *
 * @author haoxr
 * @since 2022/1/14
 */
@Service
@RequiredArgsConstructor
public class ResourcesServiceImpl extends ServiceImpl<ResourcesMapper, CarResources> implements ResourcesService {


    private final CarUserConverter userConverter;

    private final SysMenuService menuService;

    private final SysRoleService roleService;

    @Autowired
    private CarAuthMapper carAuthMapper;

    /**
     * 获取用户分页列表
     *
     * @param queryParams
     * @return
     */
    @Override
    public  IPage<CarResourcesPageVO> getResourcesPage(CarResoucesPageQuery queryParams) {

        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<CarUserPageVO> page = new Page<>(pageNum, pageSize);
        if(StringUtils.isNotEmpty(queryParams.getStartTime()) && StringUtils.isNotEmpty(queryParams.getEndTime())) {
            // 格式化为数据库日期格式，避免日期比较使用格式化函数导致索引失效
            DateUtils.toDatabaseFormat(queryParams, "startTime", "endTime");
        }

        // 查询数据
        Page<CarResourcesPageVO> resourcesPage = this.baseMapper.getResourcesPage(page, queryParams);

        // 实体转换
        return resourcesPage;
    }



    /**
     *
     * 更新用户信息
     * @return
     */
    @Override
    @Transactional
    public boolean saveResources(ResourcesForm resourcesForm) {

        // 修改用户
        boolean result = this.baseMapper.saveResources(resourcesForm);

        if("1".equals(resourcesForm.getStatus())){
            MsgDescForm msgDescForm=new MsgDescForm();
            msgDescForm.setUserId(resourcesForm.getUserId());
            msgDescForm.setMsgDesc("您发布的资源已通过审核");
            msgDescForm.setType("5");
            msgDescForm.setCreateDate(new Date());

            carAuthMapper.insertMsg(msgDescForm);
        }else{
            MsgDescForm msgDescForm=new MsgDescForm();
            msgDescForm.setUserId(resourcesForm.getUserId());
            msgDescForm.setMsgDesc("您发布的资源已被驳回，请检查数据！");
            msgDescForm.setType("6");
            msgDescForm.setCreateDate(new Date());

            carAuthMapper.insertMsg(msgDescForm);
        }
        return result;
    }

    /**
     * 删除用户
     *
     * @param idsStr 用户ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteResources(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的用户数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        return  this.baseMapper.deleteResources(ids);

    }

    @Override
    public IPage<CarResourcesImgPageVO> getResourcesImgPage(CarResoucesPageQuery queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<CarResourcesImgPageVO> page = new Page<>(pageNum, pageSize);
        if(StringUtils.isNotEmpty(queryParams.getStartTime()) && StringUtils.isNotEmpty(queryParams.getEndTime())) {
            DateUtils.toDatabaseFormat(queryParams, "startTime", "endTime");
        }
        Page<CarResourcesImgPageVO> resourcesPage = this.baseMapper.getResourcesImgPage(page, queryParams);
        return resourcesPage;
    }

    @Override
    public boolean saveResourcesImg(ResourcesImgForm resourcesImgForm) {
        return this.baseMapper.saveResourcesImg(resourcesImgForm.getImgList());
    }

    @Override
    public boolean deleteResourcesImg(String idsStr) {
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        return  this.baseMapper.deleteResourcesImg(ids);
    }

    @Override
    public List<CarResourcesExportVO> listExportResources(CarResoucesPageQuery queryParams) {
        return this.baseMapper.listExportResources(queryParams);
    }

    @Override
    public boolean saveResourcesServiceImport(CarResourcesImportVO carResourcesImportVO) {
        int result=this.baseMapper.saveResourcesServiceImport(carResourcesImportVO);
        return false;
    }


}
